<?php

!defined('IN_ROOT') && exit('Access Denied');

class bannedmodel {

    var $db;
    var $base;

    function bannedmodel(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }

    function get_ip_list($start_limit=0,$num=0) {
        $bannedlist = array();
        $this->refresh_ip();
        if(!empty($start_limit) || !empty($num)) {
            $sqladd = "LIMIT $start_limit,$num";
        }
        $query = $this->db->query("SELECT * FROM `".DB_TABLEPRE."banned` ORDER BY id DESC $sqladd");
        while($banned = $this->db->fetch_array($query)) {
            $banned['endtime'] = $this->base->date($banned['time']+$banned['expiration']);
            $banned['starttime'] = $this->base->date($banned['time']);
            if($banned['ip1']<0)$banned['ip1'] = '*';
            if($banned['ip2']<0)$banned['ip2'] = '*';
            if($banned['ip3']<0)$banned['ip3'] = '*';
            if($banned['ip4']<0)$banned['ip4'] = '*';
            $banned['ip'] = $banned['ip1'].'.'.$banned['ip2'].'.'.$banned['ip3'].'.'.$banned['ip4'];
            $bannedlist[] = $banned;
        }
        return $bannedlist;
    }

    function add_ip($alluploadips, $expiration, $username) {
        $expiration = ($expiration) ? $expiration*3600*24 : 0;
        $num = count($alluploadips);
        $start = 0;
        $step = $end = 100;
        do {
            $alluploadip = array_slice($alluploadips, $start, $step);
            $query = "INSERT INTO `".DB_TABLEPRE."banned` (`ip1`,`ip2`,`ip3`,`ip4`,`admin`,`time`,`expiration`) VALUES ";
            foreach($alluploadip as $banip) {
                $ip = explode('.', $banip);
                $query .= "('{$ip[0]}','{$ip[1]}','{$ip[2]}','{$ip[3]}','{$username}','{$this->base->time}','{$expiration}'),";
            }
            $query = substr($query,0,-1) . ';';
            $this->db->query($query);
            if($end >= $num) {
                break;
            }
            $end += $step;
            $start += $step;
        }while(true);
        $this->base->cache->remove('banned');
    }


}
?>